import {
	defineStore
} from 'pinia';
import dayjs from "dayjs";
const init = dayjs().add(0, 'day')
// 下单存储信息
export const useOrderStore = defineStore('order', {
	state: () => {
		return {
			// 下单选中的时间 默认第二天的时间
			now: {
				day: init,
				value: init.format("M/D"),
				formatTime: init.format("YYYY/MM/DD"),
				des: '',
			},
			// 支付方式
			payWay: null,
			// 仓库信息
			warehouse: null,
			// 子账号信息
			childAccount: null,
			goodsList: [], //入、出、盘商品列表
			contactOrder: null,
		};
	},
	getters: {
		// 送货日期
		deliveryDateTime: (state) => state.now.formatTime,
		// 配送时间戳
		deliveryDateTimeStr: (state) => state.now.day.valueOf(),
		payWayName: (state) => state.payWay ? state.payWay.paymentName : '',
		// 商品数量
		total: (state) => state.goodsList.length,
		// 商品总价
		totalPrice: (state) => {
			console.log(state.goodsList)
			try {
				return state.goodsList.reduce((pre, current) => {
					return Number((pre + Number(current.amount)).toFixed(2))
				}, 0)
			} catch (e) {
				return 0
			}
		},
	},
	actions: {
		/**
		 * @description 设置默认时间, 默认当天
		 * @param {*} val 数字类型, 1为明天,2位后天,以此类推
		 */
		initDateTime(val = 0) {
			const _init = dayjs().add(val, 'day')
			this.now = {
				day: _init,
				value: _init.format("M/D"),
				formatTime: _init.format("YYYY/MM/DD"),
				des: '',
			}

		},
		/**
		 * 
		 * @param {*} str 时间戳
		 */
		setDataTime(str) {
			const _init = dayjs(Number(str))
			this.now = {
				day: _init,
				value: _init.format("M/D"),
				formatTime: _init.format("YYYY/MM/DD"),
				des: '',
			}
		},
		setContactOrder(item) {
			this.contactOrder = item
		},
		setDateTime(item) {
			this.now = item
		},
		setPayWay(item) {
			this.payWay = item
		},
		setWarehouse(item) {
			this.warehouse = item
		},
		setChildAccount(item) {
			this.childAccount = item
		},
		setShopList({ data, type }) {
			if (uni.$u.test.array(data)) {
				// 数组
				data.forEach(btn => {
					this.addShopList({ data: btn, type })
				})
			} else {
				this.addShopList({ data, type })
			}
		},
		addShopList({ data, type }) {
			const item = uni.$u.deepClone(data)
			console.log(item)
			item.amount = (item.unitPrice * item.qty).toFixed(2)
			if (type === 'Count') {
				item.costPrice = item.qty
				item.billQuantity = item.unitPrice
			}
			this.goodsList.push(item)
		},
		// 判断是否已经存在
		hasShopItem(item) {
			let str = item.productSkuId + item.unitId
			for (let i = 0, len = this.goodsList.length; i < len; i++) {
				let _item = this.goodsList[i]
				let _str = _item.productSkuId + _item.unitId
				if (_str === str) {
					return true
				}
			}
			return false
		},
		updateData(item) {
			let str = item.productSkuId + item.unitId
			for (let i = 0, len = this.goodsList.length; i < len; i++) {
				let _item = this.goodsList[i]
				let _str = _item.productSkuId + _item.unitId
				if (_str === str) {
					item.amount = (item.unitPrice * item.qty).toFixed(2)
					this.goodsList[i] = item
					break;
				}
			}
			return true
		},
		deleteShopListItem({ item, index }) {
			this.goodsList = this.goodsList.filter((btn, idx) => {
				return idx !== index
			})
		},
		clearData() {
			//清楚时间
			this.now = {
				day: init,
				value: init.format("M/D"),
				formatTime: init.format("YYYY/MM/DD"),
				des: '',
			}
			// 清楚支付方式
			this.payWay = null
			this.warehouse = null
			this.childAccount = null
			this.goodsList = []
			this.contactOrder = null
		}
	},
});